local shishen = fk.CreateSkill {
  name = "th_shishen",
  max_branches_use_time = {
    ["others"] =  {
      [Player.HistoryPhase] = 1
    }
  }
}

local spec = { ---@type TrigSkelSpec<fun(self: TriggerSkill, event: RoundEvent|PhaseEvent, target: ServerPlayer, player: ServerPlayer, data: RoundData|PhaseData):any>
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = room:getOtherPlayers(player, false)
    local tos = room:askToChoosePlayers(player, {
      targets = targets,
      min_num = 1,
      max_num = 1,
      prompt = "#th_shishen-choose",
      skill_name = shishen.name
    })
    if #tos == 1 then
      event:setCostData(self, { tos = tos })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local dat = event:getCostData(self)
    local to = dat.tos[1]
    room:addTableMark(to, "th_shishen", player)
    room:setPlayerMark(player, "@th_shishen", to)
    room:handleAddLoseSkills(to, "th_shishen&", nil, false, true)
  end,
}

shishen:addLoseEffect(function(self, player, is_death)
  local room = player.room
  local to = player:getMark("@th_shishen")
  if to ~= 0 then
    room:removeTableMark(to, "th_shishen", player)
    room:setPlayerMark(player, "th_shishen", 0)
    room:handleAddLoseSkills(to, "-th_shishen&", nil, false, true)
  end
end)

shishen:addEffect(fk.GameStart, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(shishen.name)
  end,
  on_cost = spec.on_cost,
  on_use = spec.on_use,
})
shishen:addEffect(fk.EventPhaseProceeding, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(shishen.name) and player.phase == Player.Start
  end,
  on_cost = spec.on_cost,
  on_use = spec.on_use,
})

local lose = {
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:getMark("@th_shishen") ~= 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = player:getMark("@th_shishen")
    if to then
      room:removeTableMark(to, "th_shishen", player)
      room:setPlayerMark(player, "@th_shishen", 0)
      room:handleAddLoseSkills(to, "-th_shishen&", nil, false, true)
    end
  end,
}

shishen:addEffect(fk.TurnStart, lose)

Fk:loadTranslationTable{
  ["th_shishen"] = "式神",
  [":th_shishen"] = "游戏开始时或准备阶段，你可以选择一名其他角色，直到你的下回合开始，该角色的出牌阶段限一次，其可以交给你至少一张手牌，然后其摸两张牌。",

  ["#th_shishen-choose"] = "式神：请选择要与“式神”联动的角色",

  ["@th_shishen"] = "式神",
}

return shishen